package cn.acewill.yxd.yunxundian.ui.sign;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;

import cn.acewill.yxd.yunxundian.BaseActivity;
import cn.acewill.yxd.yunxundian.R;

/**
 * Created by XXX on 2017/7/6.
 */

public class MyViewPage extends BaseActivity implements ViewPager.OnPageChangeListener{

    /**
     * ViewPager
     */
    private ViewPager viewPager;

    /**
     * 装点点的ImageView数组
     */
    private ImageView[] tips;
    /**
     * 装ImageView数组
     *
     */
    private ImageView[] mImageViews;

    private String[] fileString;
    private int selectId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.photoview_page);
        String array=getIntent().getStringExtra("filepath");
        //默认第一个选中
        selectId=getIntent().getIntExtra("position",0);
        //去掉最后一个逗号
        fileString=array.substring(0,array.length()-1).split(",");
        ViewGroup group = (ViewGroup)findViewById(R.id.viewGroup);
        viewPager = (ViewPager) findViewById(R.id.viewPager);

        tips = new ImageView[fileString.length];
        for(int i=0; i<tips.length; i++){
            ImageView imageView = new ImageView(this);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(20,20));
        //    imageView.setPadding(10,0,10,0);
            tips[i] = imageView;
            if(i == 0){
                tips[i].setBackgroundResource(R.mipmap.page_indicator_focused);
            }else{
                tips[i].setBackgroundResource(R.mipmap.page_indicator_unfocused);
            }
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));
            layoutParams.leftMargin = 10;
            layoutParams.rightMargin = 10;
            group.addView(imageView,layoutParams);
        }
        //将图片装载到数组中
        mImageViews = new ImageView[fileString.length];
        for(int i=0; i<mImageViews.length; i++) {
            ImageView imageView=new ImageView(this);
            Glide.with(this).load(fileString[i]).placeholder(R.mipmap.icon_stub)
                    .error(R.mipmap.icon_error).into(imageView);
            mImageViews[i] = imageView;
        }

        //设置Adapter
        viewPager.setAdapter(new MyAdapter());
        //设置监听，主要是设置点点的背景
        viewPager.setOnPageChangeListener(this);
        //设置ViewPager的默认项
        viewPager.setCurrentItem((selectId));

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        setImageBackground(position % mImageViews.length);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    /**
     * 设置选中的tip的背景
     * @param selectItems
     */
    private void setImageBackground(int selectItems){
        for(int i=0; i<tips.length; i++){
            if(i == selectItems){
                tips[i].setBackgroundResource(R.mipmap.page_indicator_focused);
            }else{
                tips[i].setBackgroundResource(R.mipmap.page_indicator_unfocused);
            }
        }
    }

    public class MyAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return fileString.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }


        @Override
        public void destroyItem(View container, int position, Object object) {
            //小于等于3的时候产生bug
            if (fileString.length>3){
                ((ViewPager)container).removeView(mImageViews[position % mImageViews.length]);
            }


        }

        /**
         * 载入图片进去，用当前的position 除以 图片数组长度取余数是关键
         */
        @Override
        public Object instantiateItem(View container, int position) {
            try {
                ((ViewPager)container).addView(mImageViews[position % mImageViews.length], 0);
            }catch(Exception e){
                //handler something
            }
            return mImageViews[position % mImageViews.length];
        }

    }
}
